﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace VirusCleaner.Scanner
{
    public partial class FolderView : Form
    {
        [DllImport("KERNEL32", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern int GetDriveType(string lpRootPathName);
        public const int DRIVE_FIXED = 3;

        private readonly List<string> _list;
        public FolderView()
        {
            InitializeComponent();
            _list = new List<string>();
        }
        private void FrmCustom_Load(object sender, EventArgs e)
        {
            this.Top -= 150;
            this.Left -= 190;
            FillDirTree();
        }
        public static new List<string> Show()
        {
            List<string> list;
            using (var dialog = new FolderView())
            {
                list = dialog.ShowDialog();
            }
            return list;
        }
        protected new List<string> ShowDialog()
        {
            base.ShowDialog();
            return _list;
        }
        private void FillDirTree()
        {
            string[] drivers = Environment.GetLogicalDrives();
            for (int i = 0; i < drivers.Length; i++)
            {
                if (GetDriveType(drivers[i]) == DRIVE_FIXED)
                {
                    var root = new DirNode(drivers[i]);
                    treeViewDir.Nodes.Add(root);
                    AddDirs(root);
                }
            }
        }
        private static void AddDirs(TreeNode node)
        {
            try
            {
                var dir = new DirectoryInfo(GetPathFromNode(node));
                DirectoryInfo[] e = dir.GetDirectories();
                string name;
                for (int i = 0; i < e.Length; i++)
                {
                    name = e[i].Name;
                    if (!name.Equals(".") && !name.Equals(".."))
                    {
                        node.Nodes.Add(new DirNode(name));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        private static string GetPathFromNode(TreeNode node)
        {
            if (node.Parent == null)
            {
                return node.Text;
            }
            return Path.Combine(GetPathFromNode(node.Parent), node.Text);
        }
        private void TreeViewDirBeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
        {
            DirNode nodeExpanding = (DirNode)e.Node;
            if (!nodeExpanding.SubDirectoriesAdded)
            {
                AddSubDirs(nodeExpanding);
            }
        }
        private void AddSubDirs(DirNode node)
        {
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                AddDirs(node.Nodes[i]);
            }
            node.SubDirectoriesAdded = true;
        }
        private void GetAllNodes(TreeNodeCollection tnc)
        {
            foreach (TreeNode tnn in tnc)
            {
                //遍历所有TreeNode
                if(tnn.Checked)
                {             
                    //如果有父节点 并且已经选中 不在重复添加
                    if (tnn.Parent !=null && tnn.Parent.Checked)
                    {
                        return;
                    }
                    //MessageBox.Show(GetPathFromNode(tnn));
                    _list.Add(GetPathFromNode(tnn));
                }
                if (tnn.Nodes.Count > 0)
                {
                    GetAllNodes(tnn.Nodes);
                }
            }
        }
        private void BtnSelectClick(object sender, EventArgs e)
        {
            GetAllNodes(treeViewDir.Nodes);
        }

        private void TreeViewDirBeforeCheck(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Tag != null && e.Node.Index == 0)
            {
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
            }
        }

        private void TreeViewDirAfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag != null && e.Node.Index == 0)
            {
                e.Node.Checked = true;
            }
        }
    }
    public class DirNode : System.Windows.Forms.TreeNode
    {
        public DirNode()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public bool SubDirectoriesAdded;
        public DirNode(string text)
            : base(text)
        {

        }
    }
}

